/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.graph.mouse;

import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.visualization.PickSupport;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;
import java.util.Comparator;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import org.apache.log4j.Logger;
import pl.tom.social.analyzer.gui.component.SiteTopComponent;
import pl.tom.social.analyzer.jade.LocalJade;
import pl.tom.social.common.types.ConversationIDType;

/**
 *
 * @author Tom
 */
public class PopupMousePlugin extends AbstractPopupGraphMousePlugin implements MouseListener {

	private static final Logger logger = Logger.getLogger(PopupMousePlugin.class);

	public PopupMousePlugin(int modifiers) {
		super(modifiers);
	}

	public PopupMousePlugin() {
		super(MouseEvent.BUTTON3_MASK);
	}

	@Override
	protected void handlePopup(MouseEvent e) {
		final VisualizationViewer vv =
			(VisualizationViewer) e.getSource();
		Point2D p = vv.inverseViewTransform(e.getPoint());

		PickSupport pickSupport = vv.getPickSupport();
		if(pickSupport != null) {
			final Vertex v = pickSupport.getVertex(p.getX(), p.getY());
			if(v != null) {
				JPopupMenu popup = new JPopupMenu();

				setUpdateAction(popup, v);
				setShowAction(popup, v);
				setAnalyzeMenu(popup, v);

				popup.show(vv, e.getX(), e.getY());
			}
		}
	}

	private void setAnalyzeMenu(JPopupMenu popup, final Vertex v) {
		JMenu analyzeMenu = new JMenu("Analyze");
		analyzeMenu.add(new AbstractAction("Term frequency") {

			public void actionPerformed(ActionEvent e) {
//				SocialDatabaseFacade db = new SocialDatabaseFacade();
//				Site site = null;
//				Map<String, Integer> terms;
//				try {
//					site = db.findSite(v.getUserDatum("url").toString());
//					if(site != null) {
//						terms = TermFreqCounter.get(site, true);
//						SortedMap<String, Integer> sortedData = new TreeMap<String, Integer>(new ValueComparer(terms));
//						for(String term : sortedData.keySet()) {
//							System.out.println("term:" + term + " freq:" + terms.get(term));
//						}
//					}
//				} catch(Exception ex) {
//					logger.log(Level.ERROR, ex);
//				}
			}
		});
		popup.add(analyzeMenu);
	}

	private void setShowAction(JPopupMenu popup, final Vertex v) {
		popup.add(new AbstractAction("Show") {

			public void actionPerformed(ActionEvent e) {
				SiteTopComponent window = new SiteTopComponent(v.getUserDatum("url").toString());
				window.open();
				window.requestActive();
			}
		});
	}

	private void setUpdateAction(JPopupMenu popup, final Vertex v) {
		popup.add(new AbstractAction("Update") {

			public void actionPerformed(ActionEvent e) {
				LocalJade.getInstance().addFetcherAgent(v.getUserDatum("url").toString(),
					ConversationIDType.UpdateNode);
			}
		});
	}

	private class ValueComparer implements Comparator {

		private Map _data = null;

		public ValueComparer(Map data) {
			super();
			_data = data;
		}

		public int compare(Object o1, Object o2) {
			String e1 = (String) _data.get(o1);
			String e2 = (String) _data.get(o2);
			return e1.compareTo(e2);
		}
	}
}
